package cn.bdqn.repository;

import cn.bdqn.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import javax.persistence.criteria.*;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepositoryTester {
    @Resource
    UserRepository userRepository;

    @Test
    public void testSave() {
        User user = new User("jujue", "123456", 2l, 1);
        userRepository.save(user);
    }

    @Test
    public void testUpdate() {
        User user = new User(22l, "jujue2", "888888", 1l, 0);
        userRepository.save(user);
    }

    @Test
    public void testDelete() {
        userRepository.deleteById(22l);
    }

    @Test
    public void testFindById() {
        User user = userRepository.findById(2l).get();
        System.out.println(user);
    }

    @Test
    public void testFindAll() {
        List<User> list = userRepository.findAll();
        for (User user : list) {
            System.out.println(user);
        }
    }

    @Test
    public void testFindPage() {
        Pageable pageable = PageRequest.of(0, 2);
        Page<User> page = userRepository.findAll(pageable);
        System.out.println("第几页：" + (page.getNumber() + 1));
        System.out.println("每页显示多少条：" + page.getSize());
        System.out.println("总共多少条：" + page.getTotalElements());
        System.out.println("总共多少页：" + page.getTotalPages());
        System.out.println("每页显示的数据：" + page.getContent());
    }

    @Test
    public void testFindByRoleIdByHQL() {
        List<User> list = userRepository.findByRoleIdByHQL(7l);
        for (User user : list) {
            System.out.println(user);
        }
    }

    @Test
    public void testUpdateUserByHQL() {
        userRepository.updateUserByHQL("yunxu", 23l);
    }

    @Test
    public void testFindByRoleIdBySQL() {
        List<User> list = userRepository.findByRoleIdBySQL(2l);
        for (User user : list) {
            System.out.println(user);
        }
    }

    @Test
    public void testFindByUsrName() {
        List<User> list = userRepository.findByUsrName("da");
        for (User user : list) {
            System.out.println(user);
        }
    }

    @Test
    public void testFindByUsrNameLike() {
        List<User> list = userRepository.findByUsrNameLike("%da%");//查询参数必须带%
        for (User user : list) {
            System.out.println(user);
        }
    }

    @Test
    public void testFindByUsrNameAndUsrPassword() {
        List<User> list =
                userRepository.findByUsrNameAndUsrPassword("dajun", "123456");
        for (User user : list) {
            System.out.println(user);
        }
    }

    @Test
    public void testFindSpecification1() {
        //使用匿名内部类的方式，创建一个Specification的实现类，并实现toPredicate方法
        Specification<User> spec = new Specification<User>() {
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> query,
                                         CriteriaBuilder cb) {
                //1.获取比较的属性
                Path<Object> usrName = root.get("usrName");
                //2. 构造查询条件
                Predicate p1 = cb.equal(usrName, "sansan");
                return p1;
            }
        };
        List<User> list = userRepository.findAll(spec);
        for (User user : list) {
            System.out.println(user);
        }
    }

    @Test
    public void testFindSpecification2() {
        //使用匿名内部类的方式，创建一个Specification的实现类，并实现toPredicate方法
        Specification<User> spec = new Specification<User>() {
            public Predicate toPredicate(Root<User> root,
                                         CriteriaQuery<?> query,
                                         CriteriaBuilder cb) {
                //1.获取比较的属性
                Path<Object> usrName = root.get("usrName");
                //2. 构造查询条件
                Predicate p1 = cb.like(usrName.as(String.class), "%san%");
                return p1;
            }
        };
        List<User> list = userRepository.findAll(spec);
        for (User user : list) {
            System.out.println(user);
        }
    }

    @Test
    public void testFindSpecification3() {
        //使用匿名内部类的方式，创建一个Specification的实现类，并实现toPredicate方法
        Specification<User> spec = new Specification<User>() {
            public Predicate toPredicate(Root<User> root,
                                         CriteriaQuery<?> query,
                                         CriteriaBuilder cb) {
                //1.获取比较的属性
                Path<Object> usrName = root.get("usrName");
                Path<Object> usrPassword = root.get("usrPassword");
                //2. 构造查询条件
                Predicate p1 = cb.equal(usrName, "sansan");
                Predicate p2 = cb.equal(usrPassword, "123456");
                //3.多条件连接
                Predicate and = cb.and(p1, p2);
                return and;
            }
        };
        User user = userRepository.findOne(spec).get();
        System.out.println(user);
    }

    @Test
    public void testFindSpecification4() {
        //构造分页条件
        //页数是从0开始
        Pageable pageable = PageRequest.of(0, 2);
        //构造过滤条件
        Specification<User> specification = new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?>
                    criteriaQuery, CriteriaBuilder cb) {
                //1.获取查询的属性
                Path<Object> usrRoleId = root.get("usrRoleId");
                //2.构造查询条件
                Predicate p1 = cb.equal(usrRoleId, 2l);
                return p1;
            }
        };
        //返回的值就是分页对象
        Page<User> page = userRepository.findAll(specification, pageable);
        System.out.println("第几页：" + (page.getNumber() + 1));
        System.out.println("每页显示多少条：" + page.getSize());
        System.out.println("总共多少条：" + page.getTotalElements());
        System.out.println("总共多少页：" + page.getTotalPages());
        System.out.println("当前页显示的数据：" + page.getContent());
    }

    @Test
    public void testFindSpecification5() {
        //构造过滤条件
        Specification<User> specification = new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root,
                                         CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
                //1.获取查询的属性
                Path<Object> usrRoleId = root.get("usrRoleId");
                //2.构造查询条件
                Predicate dajun = cb.equal(usrRoleId, 2l);
                return dajun;
            }
        };
//构造排序条件
        Sort sort = new Sort(Sort.Direction.DESC, "usrId");
        //构造分页条件
        //页数是从0开始
        Pageable pageable = PageRequest.of(0, 2, sort);
        //返回的值就是分页对象
        Page<User> page = userRepository.findAll(specification, pageable);
        System.out.println("第几页：" + (page.getNumber() + 1));
        System.out.println("每页显示多少条：" + page.getSize());
        System.out.println("总共多少条：" + page.getTotalElements());
        System.out.println("总共多少页：" + page.getTotalPages());
        System.out.println("当前页显示的数据：" + page.getContent());
    }
}
